package com.tmsps.ne4weixin.api;

import com.alibaba.fastjson2.JSONObject;
import com.tmsps.ne4weixin.bean.qrcode.ShortUrl;
import com.tmsps.ne4weixin.config.WxConfig;
import com.tmsps.ne4weixin.utils.WXHttpUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @Title: ShortUrlAPI.java
 * @Package: com.tmsps.ne4weixin.api
 * @Description: 长链接转短链接API
 */
@Slf4j
public class ShortUrlAPI extends BaseAPI {

	private String accessToken = null;
	
	public ShortUrlAPI(String accessToken) {
		super(null);
		this.accessToken = accessToken;
	}
	
	public ShortUrlAPI(WxConfig config) {
		super(config);
		this.accessToken = config.getAccessToken();
	}
	
	/**
	 * 长链接转短链接
	 * @param longUrl
	 * @return
	 */
	public ShortUrl getShortUrl(String longUrl){
		JSONObject param = new JSONObject();
		param.put("action", "long2short");
		param.put("long_url", longUrl);
		String result = WXHttpUtil.postJson(String.format(SHORT_URL, accessToken), param.toJSONString());
		log.info("长链接转短链接返回信息：{}",result);
		return JSONObject.parseObject(result, ShortUrl.class);
	}
}
